/**
 * @author LKQ
 * @date 2021/12/22 17:55
 * @description 但是不排序也可以实现。
 */
public class NoSortSolution {
    public static void main(String[] args) {
        NoSortSolution solution = new NoSortSolution();
        int[] heights = {1, 1, 4, 2, 1, 3};
        System.out.println(solution.heightChecker(heights));
    }

    public int heightChecker(int[] heights) {
        int max = 0;
        for (int height : heights) {
            if (height > max) {
                max = height;
            }
        }
        int[] arr = new int[max+1];
        for (int height : heights) {
            arr[height]++;
        }
        int count = 0;
        for (int i = 1, j = 0; i < arr.length; i++) {
            while (arr[i]-- > 0) {
                if (heights[j++] != i) {
                    count++;
                }
            }
        }
        return count;
    }
}
